﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Etapa1.SofRender
{
    public class GeoShaderShader : SEffect,PixelShader,VertexShader,GeometricShader
    {
        public GeoShaderShader()
        {
            this.GeometricShader = this;
            this.PixelShader = this;
            this.VertexShader = this;
        }

        Matrix world;

        public Matrix World
        {
            get { return world; }
            set { world = value; }
        }
        Matrix view;

        public Matrix View
        {
            get { return view; }
            set { view = value; }
        }
        Matrix projection;

        public Matrix Projection
        {
            get { return projection; }
            set { projection = value; }
        }        

        public Texture2D Difuse
        {
            set { sampler = TextureSampler.Sampler(value);  }
        }

        TextureSampler sampler;

        #region VertexShader Members

        VertexType VertexShader.VertexShader(VertexType vertice)
        {            
            return vertice;
        }

        #endregion

        #region PixelShader Members

        Color PixelShader.PixelShader(VertexType vo)
        {
            Vector4 vec = vo[Slots.TEXTCOORD0];
            return sampler.Text2D(vo[Slots.TEXTCOORD0]);            
        }

        #endregion

        #region GeometricShader Members

        public VertexType[] GShader(PrimitivesType inPrimType, VertexType[] vertice, ref PrimitivesType outPrimType)
        {
            outPrimType = PrimitivesType.TriangleList;
            VertexType[] resp = new VertexType[9];
            int i = 0;
            ///Cada ponto vira 3 triangulos
            foreach (var item in vertice)
            {                                
                VertexType v0 = item.Clone();
                VertexType v1 = item.Clone();
                VertexType v2 = item.Clone();
                Vector4 p0 = v0[Slots.POSITION0];
                p0.X += 0.1f;
                v0[Slots.POSITION0] = p0;

                p0 = v1[Slots.POSITION0];
                p0.X -= 0.1f;
                v1[Slots.POSITION0] = p0;

                p0 = v2[Slots.POSITION0];
                p0.Y += 0.1f;
                v2[Slots.POSITION0] = p0;

                resp[i++] = v0;                
                resp[i++] = v1;
                resp[i++] = v2;
            }
            return resp;
        }

        #endregion
    }
}
